import Vue from 'vue'
import toast from './HelloWorld.vue'
// 动态创建组件
function showToast(content, duration = 2000) {
    return new Promise((resolve) => {
        const ToastConstructor = Vue.extend(toast);
        let confirmDom = new ToastConstructor({
            el: document.createElement('div'),
            data() {
                return {
                    show: true,
                    content
                };
            }
        });
        document.body.appendChild(confirmDom.$el);
        removeDom(confirmDom, duration);
        resolve()
        // confirmDom.confirm = list => {
        //     resolve(list);
        //     removeDom(confirmDom, duration);
        // };
        // confirmDom.cancel = () => {
        //     removeDom(confirmDom, duration);
        // };
    });
}
// 关闭并销毁dom
function removeDom(dom, duration) {
    // setTimeout(() => {
    //     // dom.content.show = false;
    //     console.log(dom,duration)
    //     dom.show = false;
    //     dom.$destroy();
    //     // if (document.body.contains(dom.$el.parentNode)) {
    //     //     document.body.removeChild(dom.$el.parentNode);
    //     // }
    // }, duration)
    setTimeout(() => {
        dom.show = false;
        document.body.removeChild(dom.$el);
        dom.$destroy();//完全销毁一个实例
        dom = null;
    }, duration);
}

function createExtend() {
    Vue.prototype.$toast = showToast;
}
export default createExtend;